package dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import domain.Message;
import domain.User;

public class MessageDaoImpl implements MessageDao {
	private SessionFactory sessionFactory;

	
	
	




	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public void insertMessage(String messageContent, String recId, String acc,boolean b) {
		// TODO Auto-generated method stub
		try{
		Session session = sessionFactory.getCurrentSession();
		Query q = session.createQuery("from User where account = ?");
		q.setString(0, acc);
		List userList = q.list();
		User sendUser = null;
		User recUser = null;
		int recIdInt = 0;
		if(userList.size()>=1)
			sendUser = (User) userList.get(0);
		
		if(!b){
			if(recId!=null)
				recIdInt = Integer.parseInt(recId);
		recUser = (User) session.load(User.class, recIdInt);
		}else{
			q = session.createQuery("from User where name = ?");
			q.setString(0, recId);
			List list = q.list();
			if(list.size()>=1){
				recUser = (User) list.get(0);
			}
		}
		Message m = new Message();
//		m.setId(5555);
		m.setContent(messageContent);
		m.setSendName(sendUser.getName());
		m.setRecName(recUser.getName());
		m.setSend_date(new Date());
		session.saveOrUpdate(m);
		recUser.getRecMess().add(m);
		sendUser.getSendMess().add(m);
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

	@Override
	public List<Message> showSendMessage(String acc) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query q = session.createQuery("from User where account = ?");
		q.setString(0, acc);
		User user = null;
		List userList = q.list();
		int recIdInt = 0;
		List<Message> sendMessages = null;
		if(userList.size()>=1)
			user = (User) userList.get(0);
		if(user!=null)
			sendMessages = user.getSendMess();
		if(sendMessages.size()>1)
			sendMessages.get(0);
		return sendMessages;
	}

	@Override
	public void messageDao(String[] mIds) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		List<Message> ms = new ArrayList();
		Message m = null;
		for(int i=0; i<mIds.length; i++){
			m = new Message();
			if(mIds[i]!=null){
				m.setId(Integer.parseInt(mIds[i]));
			}
			ms.add(m);
		}
		Iterator<Message> it = ms.iterator();
		while(it.hasNext()){
			session.delete(it.next());
		}
	}

	@Override
	public List<Message> showRecMessage(String acc) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query q = session.createQuery("from User where account = ?");
		q.setString(0, acc);
		User user = null;
		List userList = q.list();
		int recIdInt = 0;
		List<Message> recMessages = null;
		if(userList.size()>=1)
			user = (User) userList.get(0);
		if(user!=null)
			recMessages = user.getRecMess();
		if(recMessages.size()>=1)
			recMessages.get(0);
		return recMessages;
	}


}
